package tests;

import static org.junit.Assert.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import model.Email;
import model.EmailBuilder;
import model.Folder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * This class tests the Folder class
 * 
 * @author buthainahaldosary
 * @since May 2013
 *
 * @version $Revision: 1.0 $
 */
public class TestFolder {

	Folder parentFolder,childFolder;
	Email email1,email2;
	Date date;
	int uid=2;
	Timestamp currentTime;
	EmailBuilder emb=new EmailBuilder();
	ArrayList<Email> emails;
	
	/**
	 * This method is called before every test case it initializes a parent and child folder and 
	 * two email objects. It then adds these emails to an array list and assigns it to the parent folder.
	 */
	
	@Before
	public void setUp()
	{
		parentFolder=new Folder("parent folder", 1,0);
		parentFolder.setId(3);
		childFolder=new Folder("child folder", 1,3);
		childFolder.setId(4);
		 date= new Date();
		 currentTime=(new Timestamp(date.getTime()));
		 email1=emb.setTo("Sara@gmail.com").setFrom("Buthainah.aldosary@gmail.com").setSubject("Hello").setBody("Just dropping by to say hello")
				 .setRead(false).setTimestamp(currentTime).setFolderId(3).build();
		 email1=emb.setTo("Laura@gmail.com").setFrom("Buthainah.aldosary@gmail.com").setSubject("Hey").setBody("How are you?")
				 .setRead(false).setTimestamp(currentTime).setFolderId(3).build();
		 emails=new ArrayList<Email>();
		emails.add(email1);
		emails.add(email2);
		parentFolder.setEmails(emails);
	}
	
	/**
	 * This method is called after every test case to tear down all the variables and force garbage collection.
	 */
	@After
	public void tearDown()
	{
		parentFolder=null;
		childFolder=null;
		email1=null;
		email2=null;
		emb=null;
		System.gc();
		
	}

	/**
	 * This test ensures that transfer object for folder is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetFolderHashMap() {
		@SuppressWarnings("serial")
		HashMap<String, String> folderTransferObject = new HashMap<String, String>(){{
			put("Id", String.valueOf(3));
			put("Name", "parent folder");
			put("UserId", String.valueOf(1));
			put("ParentId", String.valueOf(0));
		}};
		
		assertEquals(folderTransferObject, parentFolder.getTransferObject());		
	}
	
	/**
	 * This test ensures that transfer object is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetEmptyHashMap() {
		HashMap<String, String> folderTransferObject = new HashMap<String, String>();
		assertFalse(folderTransferObject.equals(parentFolder.getTransferObject()));
	}
	
		
	/**
	 * This test case tests the getId method by ensuring that the id is equal to the one specified.
	 */
	@Test
	public void testGetId() {
	assertEquals(3,parentFolder.getId());
	}
	/**
	 * This test case tests the setId() method by setting a new Id and ensuring that the folder id equals the new id.
	 */
	@Test
	public void testSetId() {
		parentFolder.setId(5);
		assertEquals(5,parentFolder.getId());
	}

	/**
	 * This test case tests the getName method by ensuring that the name is equal to the one specified.
	 */
	@Test
	public void testGetName() {
		assertEquals("parent folder",parentFolder.getName());
	}

	/**
	 * This test case tests the setName() method by setting a new Name and ensuring that the folder Name equals the new Name.
	 */
	@Test
	public void testSetName() {
		parentFolder.setName("new name");
		assertEquals("new name",parentFolder.getName());
	}

	/**
	 * This test case tests the getUserId method by ensuring that the name is equal to the one specified.
	 */
	@Test
	public void testGetUserId() {
		assertEquals(1,parentFolder.getUserId());
	}

	/**
	 * This test case tests the setUserId() method by setting a new UserId and ensuring that the folder UserId equals the new UserId.
	 */
	@Test
	public void testSetUserId() {
		parentFolder.setUserId(2);
		assertEquals(2,parentFolder.getUserId());
		
	}

	/**
	 * This test case tests the getParentId method by ensuring that the parent id is equal to the one specified.
	 */
	@Test
	public void testGetParentId() {
		assertEquals(3,childFolder.getParentId());
	}

	/**
	 * This test case tests the setParentId() method by setting a new ParentId and ensuring that the folder ParentId equals the new ParentId.
	 */
	@Test
	public void testSetParentId() {
		childFolder.setParentId(6);
		assertEquals(6,childFolder.getParentId());
	}

	/**
	 * This test case tests the getEmails method by ensuring that the size of the array list equals the one specified.
	 */
	@Test
	public void testGetEmails() {
		ArrayList<Email> result=(ArrayList<Email>) parentFolder.getEmails();
		assertEquals(2,result.size());
	}


}
